using Avalonia.Controls;
using Avalonia.Interactivity;
using System.Threading.Tasks; // Required for Task
using Avalonia; // Required for Application

namespace DesktopApp.Views
{
    public partial class ConfirmationDialog : Window
    {
        public ConfirmationDialog()
        {
            InitializeComponent();
#if DEBUG
            this.AttachDevTools(); // Attach DevTools in debug builds
#endif
        }

        // Static method to show the dialog easily
        // Takes owner window, title, and message
        public static async Task<bool> ShowAsync(Window? owner, string title, string message)
        {
            var dialog = new ConfirmationDialog
            {
                Title = title
            };

            // Find the TextBlock by Name and set its Text property
            var messageTextBlock = dialog.FindControl<TextBlock>("MessageTextBlock");
            if (messageTextBlock != null)
            {
                messageTextBlock.Text = message;
            }

            // ShowDialog requires the owner window
            if (owner is null)
            {
                 // Fallback or error handling if owner is null
                 // Maybe log an error, or try showing non-modally? For confirmation, owner is preferred.
                 System.Diagnostics.Debug.WriteLine("ConfirmationDialog Error: Owner window is null.");
                 return false; // Indicate cancellation or failure
            }
            
            // Show the dialog and wait for it to close, returning the boolean result
            return await dialog.ShowDialog<bool>(owner);
        }

        private void ConfirmButton_Click(object? sender, RoutedEventArgs e)
        {
            Close(true); // Close the dialog and return true
        }

        private void CancelButton_Click(object? sender, RoutedEventArgs e)
        {
            Close(false); // Close the dialog and return false
        }
    }
} 